package org.dromara.work.service.impl;

import org.dromara.work.service.ISpatialAttrBaseService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author fu-denghui
 * CreateTime 2023/2/3 8:57
 */
@Component
public class SpatialAttrFactory implements ApplicationContextAware {

    private static Map<List<String>, ISpatialAttrBaseService> geoAttrBaseServiceMap;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, ISpatialAttrBaseService> map = applicationContext.getBeansOfType(ISpatialAttrBaseService.class);
        geoAttrBaseServiceMap = new HashMap<>();
        map.forEach((key, value) -> geoAttrBaseServiceMap.put(value.getDmlb(), value));
    }

    public static <T extends ISpatialAttrBaseService> T getGeoAttrBaseService(String dmlb) {
        if (null != geoAttrBaseServiceMap && geoAttrBaseServiceMap.size() > 0) {
            for (Map.Entry<List<String>, ISpatialAttrBaseService> entry : geoAttrBaseServiceMap.entrySet()) {
                List<String> list = entry.getKey();
                if (list.contains(dmlb)) {
                    return (T)entry.getValue();
                }
            }
        }
        return null;
    }
}
